﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GIVisualEditor
{
    public partial class StateViewControl : TextBox
    {
        public const int BUTTONDOWN  = 0xA1;
        public const int CAPTION = 2;
        private Node associatedNode = null;

        public Node AssociatedNode
        {
            get { return associatedNode; }
        }




        private DirectedGraph graph = null;

        public StateViewControl(DirectedGraph g, Node n)
        {
            this.graph = g;
            associatedNode = n;
            InitializeComponent();
            this.MouseDown += new MouseEventHandler(StateViewControl_MouseDown);
            this.GotFocus += new EventHandler(StateViewControl_GotFocus);
            this.LostFocus += new EventHandler(StateViewControl_LostFocus);
            this.LocationChanged += new EventHandler(StateViewControl_LocationChanged);
        }

        void StateViewControl_LocationChanged(object sender, EventArgs e)
        {
            if (this.graph != null) this.graph.ReDraw();
        }


        void StateViewControl_LostFocus(object sender, EventArgs e)
        {
            // check if shift button is pressed, if not then reset the selection state
            if (Control.ModifierKeys == Keys.Shift)
            {
                if (graph != null && graph.TaskBag != null && associatedNode != null)
                {
                    int i = graph.TaskBag.FindIndex(N => N == associatedNode);
                    if (i >= 0)
                    {
                        //this.Text = "Do nothing";// DO nothing
                    }
                    else
                    {
                        graph.TaskBag.Add(associatedNode);
                    }
                }
            }
        }

        void StateViewControl_GotFocus(object sender, EventArgs e)
        {
            if (Control.ModifierKeys == Keys.Shift)
            {
                if (graph != null && graph.TaskBag != null && associatedNode != null)
                {
                    int i = graph.TaskBag.FindIndex(N => N == associatedNode);
                    if (i >= 0)
                    {
                        //this.Text = "Do nothing";// DO nothing
                    }
                    else
                    {
                        graph.TaskBag.Add(associatedNode);
                    }
                }
                //else this.Text = "No Action";
            }
            else
            {
                //this.Text = "No Shift";
            }

        }



       

        void StateViewControl_MouseDown(object sender, MouseEventArgs e)
        {
            if( e.Button == System.Windows.Forms.MouseButtons.Left){
                this.Capture = false;
                Message msg  = Message.Create(this.Handle, BUTTONDOWN, new IntPtr(CAPTION), IntPtr.Zero);
                this.DefWndProc(ref msg);
            }
        }

    }
}
